/** axios 公共配置
 * @desc:
 * @author zhangqiang
 * @date 2022/2/7
*/
import axios from "axios";
import { Message, MessageBox,Notification } from "element-ui";
import router from '../../../../router'
import qs from 'qs'
import { clearStoreSession } from "@/assets/common/js/global";//引入清除缓存
let requestURL="/api/v1"
// 创建axios实例
const service = axios.create({
    // 整个页面的baseURL在这里配置其余的页面接口只需要接口地址
    baseURL: process.env.NODE_ENV == 'development' ? process.env.VUE_APP_BASE_URL : requestURL,
    timeout: -1, // 请求超时时间
    headers: {
        "api-version": "1"
    }
});
// request拦截器
service.interceptors.request.use(
    config => {
        let token = localStorage.getItem("token")||'222';
        if (token) {
            if (config.upFile) {
                // config.headers['Content-Type'] = config.resheaders['Content-Type']
                config.headers['Content-Type'] = "multipart/form-data;"
                delete config.headers.Authorization
            } else {
                if (config.formdataType) {
                    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
                    config.data = qs.stringify(config.data)
                } else {
                    //json类型
                    config.headers['Content-Type'] = 'application/json';
                }
                // config.headers["Authorization"] = "Bearer " + token; // 让每个请求携带自定义token
            }

        } else {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            config.data = qs.stringify(config.data)
        }
        return config;

    },
    error => {
        Promise.reject(error);
    }
);
function NotificationTips(msg){
    Notification.closeAll()
    Notification({
        message: msg,
        type: "warning",
        duration: 3 * 1000,
        showClose:true,
        customClass:'customNotifyClass'
    });
}
let messageBoxFlag = 0 // 默认 MessageBox 未打开
// respone拦截器
service.interceptors.response.use(
    response => {
        const res = response.data;
        // 请求状态码非200统一处理
        if (res.code && res.code != 200) {
            if (document.getElementsByClassName("el-message").length == 0) {
                if(res.msg!="未知错误"){
                    NotificationTips(res.msg)
                }

                // Message({
                //     message: res.msg,
                //     type: "warning",
                //     duration: 3 * 1000,
                //     showClose:true
                // });
            }
            // 状态码非200 中的状态码为401的处理
            if (res.code == 401) {
                // 后台返回token超时需要重新登录
                clearStoreSession()
                localStorage.removeItem("token");
                router.push({
                    path: '/login',
                });
            }
            return Promise.reject("error");
        }
        return response.data;
    },
    error => {
        console.log('error',error.message);
        if (!error.response && error.message.includes('timeout')) {   // 判断请求异常信息中是否含有超时timeout字符串代表是请求超时引起的错误
            NotificationTips('请求超时，请稍后再试')
            // Message({
            //   message: "请求超时，请稍后再试",
            //   type: "warning",
            //   duration: 3 * 1000,
            //     showClose:true
            // });
            return Promise.reject(error);          // reject这个错误信息
        }else if(!error.response && error.message.includes('Network Error')){
            // NotificationTips('接口服务异常！')
            // MessageBox.confirm('接口服务异常！', {
            //     confirmButtonText: '重试',
            //     // showClose:false,
            //     showCancelButton:false,
            //     closeOnClickModal:false,
            //     type: 'error'
            // }).then(() => {
            //     location.reload()
            // })


            // this.W.$tips("warning", "Network Error");
        }
        if (error.response.status === 401) {
            // 后台返回token超时需要重新登录
            if (document.getElementsByClassName("el-message").length == 0) {
                NotificationTips('授权已过期，跳转到登录页')
                // Message({
                //     message: '授权已过期，跳转到登录页',
                //     type: "warning",
                //     duration: 3 * 1000,
                //     showClose:true
                // });
            }
            localStorage.removeItem('token');
            router.push({
                path: '/login',
            });
        }

        return Promise.reject(error);
    }
);
export default service;
